package westh.ilib.struts.action.myTagCloud;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.DynaActionForm;
import org.mule.extras.client.MuleClient;
import org.mule.umo.UMOMessage;

import westh.config.Config;
import westh.ilib.bean.tagBean.TagSys;

/**
 * @author ChaiLinglu
 * @ModifyDate 2010-09-07
 * @modifier ChaiLinglu
 * @content refine
 * @modified zhangjian
 * @date 2010-09-16
 * @content modified clouds address
 */
public class GetRscByTagAction extends Action {
	private final static Log log = LogFactory.getLog(GetRscByTagAction.class);
	//get clouds Ip address
	String localhostUrl = Config.get("localhostUrl");
	String remoteUrl = Config.get("remoteUrl");
	
	public ActionForward execute(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response) {
		DynaActionForm f = (DynaActionForm) form;
		//get data from page
		String rscTag=f.getString("tags");
		String like=f.getString("like");//1:fuzzy      0:exact
		String userId=request.getRemoteUser();
		userId="ebb70a6400e64872ad24fe5a9da30c2c";///////////////////////////////////////////////////tempTest
		
		//create input json for tagsysCloud
		JSONObject tagInput=new JSONObject();
		tagInput.put("rscTag", rscTag);
		tagInput.put("userId", userId);
		
		String outTagString = "";
		if("0".equals(like)){//exact
			//call webService   --  tagsysCloud
			try {
				MuleClient clientD = new MuleClient();
				UMOMessage umo = clientD.send("axis:http://"+remoteUrl+":4481/DataCenter/TagDataCenter?method=getRscListByTag",tagInput.toString(), null);
				outTagString = (String) umo.getPayload();
				log.debug("getRscListByTag:OUTPUT:tagsysCloud:"+outTagString);
			} catch (Exception e) {
				throw new RuntimeException(e);
			}
		}else{//fuzzy
			//call webService   --  tagsysCloud
			try {
				MuleClient clientD = new MuleClient();
				UMOMessage umo = clientD.send("axis:http://"+remoteUrl+":4481/DataCenter/TagDataCenter?method=getLikeRscByTag",tagInput.toString(), null);
				outTagString = (String) umo.getPayload();
				log.debug("getLikeRscByTag:OUTPUT:tagsysCloud:"+outTagString);
			} catch (Exception e) {
				throw new RuntimeException(e);
			}
		}
		

		String retCode=""; //return code
		String retMsg="";//return message
		String returnStr="";//
		
		JSONObject jsonOut= JSONObject.fromObject(outTagString);
		retCode = jsonOut.getString("retCode");
		if (retCode.equals("5500")) {
			String result = jsonOut.getString("result");
			JSONArray resultArry = JSONArray.fromObject(result);
			log.info("JSONArray SIZE:="+resultArry.size());

			TagSys tagsys = new TagSys();
			List list = new ArrayList();
			for(int i=0;i<resultArry.size();i++){
				tagsys = new TagSys();
				JSONObject jsonOut2 = JSONObject.fromObject(resultArry.get(i));
				String rscId=jsonOut2.getString("rscId");
				
				//create input json for CDO
				JSONObject inJson = new JSONObject();  //rulesControl
				JSONObject inStr = new JSONObject();   //direct call
				inStr.put("rscId", rscId);
				
				inJson.put("methodName", "getRscInfoById");//rulesControl
				inJson.put("jsonStr", inStr);//rulesControl
				
				//call webService  --  CDO
				String outString = "";
				try {
					MuleClient clientD = new MuleClient();
					UMOMessage umo = clientD.send("axis:http://"+localhostUrl+":4381/LIBJson/iDlibJson?method=getRscInfoById",inStr.toString(), null);
					outString = (String) umo.getPayload();
					log.debug("getRscInfoById:OUTPUT:CDO:"+outString);
				} catch (Exception e) {
					throw new RuntimeException(e);
				}
				
				JSONObject jsonCDOOut= JSONObject.fromObject(outString);
				retCode = jsonCDOOut.getString("retCode");
				tagsys.setAvgScore(jsonOut2.getString("avgScore"));////////////////////////////////////rscAvgScore
				if(retCode.equals("0")){
					String resultCDO = jsonCDOOut.getString("result");
					JSONObject resultJSONCDO = JSONObject.fromObject(resultCDO);
					String type="";
					if(resultJSONCDO.getString("rscType").equals("1")){
						type="图书";
					}
					if(resultJSONCDO.getString("rscType").equals("2")){
						type="会议论文";
					}
					if(resultJSONCDO.getString("rscType").equals("3")){
						type="期刊论文";
					}
					if(resultJSONCDO.getString("rscType").equals("4")){
						type="专利";
					}
					if(resultJSONCDO.getString("rscType").equals("5")){
						type="标准";
					}
					if(resultJSONCDO.getString("rscType").equals("6")){
						type="软著";
					}
					if(resultJSONCDO.getString("rscType").equals("7")){
						type="技术报告";
					}
					tagsys.setRscId(resultJSONCDO.getString("rscId"));
					tagsys.setRscTitle(resultJSONCDO.getString("rscTitle"));
					tagsys.setRscSummary(resultJSONCDO.getString("rscAbstract"));
					tagsys.setRscPublishDate(resultJSONCDO.getString("rscPublishDate"));
					tagsys.setRscAuthor(resultJSONCDO.getString("rscCreator"));
					tagsys.setRscType(type);
					tagsys.setField(resultJSONCDO.getString("rscField"));
					tagsys.setRscDate(resultJSONCDO.getString("rscPublishDate"));
					tagsys.setRscSource(resultJSONCDO.getString("rscSource"));
					list.add(tagsys);
				}else{
					retMsg = jsonOut.getString("retMsg");
					request.setAttribute("info", retMsg);
					returnStr = "fail";
				}
			}
			request.setAttribute("list", list);
			
		}else{
			retMsg = jsonOut.getString("retMsg");
			request.setAttribute("info", retMsg);
			returnStr = "fail";
		}
		
    	request.setAttribute("tagName", rscTag);
		request.setAttribute("totalCount", "1");/////////////////////////////////tempTest
		return mapping.findForward("succ");
	}
}